# coding: utf-8
"""
获取文章信息
"""
import glob
import json
import os
import random
import sqlite3
import time
import traceback
from datetime import datetime

import requests
from tqdm import tqdm
from wechatarticles import ArticlesInfo

from wechatarticles.utils import (
    base_columns,
    copyright_num,
    mode_columns,
    transfer_url,
    verify_url,
)

"""
1: 阅读点赞
2: 评论
3: 正文内容
4: 阅读点赞+评论
5: 阅读点赞+正文内容
6: 评论+正文内容
7: 阅读点赞+评论+正文内容
"""

dbname = "dbname"
with open("params/cookie_token.txt", "r", encoding="utf-8") as f:
    data = f.readlines()
# biz_name_key里面的name
with open("params/nickname.txt", "r", encoding="utf-8") as f:
    fj_lst = f.readlines()
fj_lst = [fj.strip() for fj in fj_lst if fj.strip() != ""]
cookie, appmsg_token = (item.strip() for item in data)
test = ArticlesInfo(appmsg_token, cookie)

# 阅读点赞
columns = mode_columns[4]
# columns = base_columns
# 原创
copyright_condition = False
# 500条内
count_condition, count_num = False, 500
# 日期->时间戳
timestamp_condition = False
# 日期精确到秒
timesec_condition = False
# 指定日期
date_condition = False

dt2stamp = lambda dt: time.mktime(time.strptime(dt, "%Y-%m-%d"))
dt = "2020-05-01"
end_timestamp = dt2stamp(dt)
# end_timestamp = time.time()  # 默认终止日期为当前时间

dt = "2020-01-01"
start_timestamp = dt2stamp(dt)


def get_read_like(url):
    read_num, like_num, old_like_num = test.read_like_nums(url)
    return read_num, old_like_num, like_num


def get_commensts(url):
    comment_json = test.comments(transfer_url(url))
    if "elected_comment" in comment_json.keys():
        comments_content = ""
        comments_like_num = ""
        comments_num = len(comment_json["elected_comment"])
        for i in comment_json["elected_comment"]:
            comments_content += "{}--".format(i["content"])
            comments_like_num += "{}--".format(i["like_num"])
        # comments_content = comments_content[:-2]
        # comments_like_num = comments_like_num[:-2]

        # for item in comment_json['elected_comment']:
        #     comments_content += '{}-{}-{}--'.format(item['nick_name'],
        #                                             item['content'],
        #                                             item['like_num'])
    else:
        comments_num = 0
        comments_content = ""
        comments_like_num = 0
    return comments_num, comments_content, comments_like_num


def insert_sqlite(conn, *base_lst):
    if "read_num" in columns:
        read_like_lst = get_read_like(url)
    else:
        read_like_lst = 0, 0, 0

    if "comments_num" in columns:
        comments_lst = get_commensts(url)
    else:
        comments_lst = 0, "", ""
    if "content" in columns:
        content_lst = list(test.content(url))
    else:
        content_lst = ["", 0, 0]

    try:
        conn.execute(
            insert_command, (*base_lst, *read_like_lst, *comments_lst, *content_lst)
        )
    except Exception as e:
        print(e, url)
        content_lst[0] = ""
        conn.execute(
            insert_command, (*base_lst, *read_like_lst, *comments_lst, *content_lst)
        )
    finally:
        conn.commit()

def timestamp2date(timestamp):
    if date_condition and not (start_timestamp <= timestamp <= end_timestamp):
        return ""
    if timestamp_condition == True:
        return str(timestamp)
    # ymd = time.localtime(timestamp)
    # date = '{}-{}-{}'.format(ymd.tm_year, ymd.tm_mon, ymd.tm_mday)
    base_f = datetime.fromtimestamp(timestamp)
    date = base_f.strftime("%Y-%m-%d")
    if timesec_condition == True:
        date = base_f.strftime("%Y-%m-%d %H:%M:%S")
        # date = time.strftime("%Y-%m-%d %H:%M:%S", ymd)
        # date = time.strftime("%Y年%m月%d日%H时%M分%S秒", ymd)
        return date
    return date


def info2data(item, head):
    url = item["content_url"]
    if (not verify_url(url)) or (url in haved_url_lst):
        return ["", "", "", ""]
    copyright_stat = copyright_num(item["copyright_stat"])
    if copyright_condition and copyright_stat != 1:
        return ["", "", "", ""]
    return [url, item["title"], head, copyright_stat]


create_tabel_command = """CREATE TABLE '{}' (
                url               KEY     NOT NULL,
                title             TEXT    NOT NULL,
                date              TEXT    NOT NULL,
                headlines         INT,
                copyright         INT,
                read_num          INT,
                old_like_num      INT,
                like_num          INT,
                comments_num      INT,
                comments_content  TEXT,
                comments_like_num TEXT,
                content           TEXT,
                content_num       INT,
                pic_num           INT
                );"""

if __name__ == "__main__":
    if len(fj_lst) == 1 and dbname == "":
        dbname = fj_lst[0]
    elif len(fj_lst) > 1 and dbname == "":
        print("请输入dbname")
        assert 1 == 2
    for fj in fj_lst:
        conn = sqlite3.connect("{}.db".format(dbname), check_same_thread=False)

        insert_command = (
            """INSERT INTO '%s' (
        url, title, date, headlines, copyright, read_num, old_like_num, like_num, 
        comments_num, comments_content, comments_like_num, 
        content, content_num, pic_num) \
        VALUES (
            ?,?,?,?,?,?,?,?,
            ?,?,?,
            ?,?,?
        )
        """
            % fj
        )
        try:
            conn.execute(create_tabel_command.format(fj))
            haved_url_lst = []
        except Exception as e:
            print(e)
            print("已有db")
            x = list(conn.execute("SELECT url FROM '{}'".format(fj)))
            haved_url_lst = list(map(lambda item: item[0], x))
            # print(len(haved_url_lst))

        with open(fj + ".json", "r", encoding="utf-8") as f:
            data = f.readlines()
        ccc = 0
        print(fj, len(data))
        flag = 0
        for line in tqdm(data):
            # for i, line in enumerate(data, 0):
            #     print("index:", i)
            item = json.loads(line.strip()[1:-1], strict=False)
            date = timestamp2date(item["comm_msg_info"]["datetime"])
            if date == "":
                continue
            infos = item["app_msg_ext_info"]
            url_title_lst = [info2data(infos, 1)]
            if infos["is_multi"] == 1:
                for i, info in enumerate(infos["multi_app_msg_item_list"], 2):
                    url_title_lst.append(info2data(info, i))

            for url, title, headlines, copyright_stat in url_title_lst:
                try:
                    if url == "":
                        continue
                    haved_url_lst.append(url)
                    ccc += 1
                    if count_condition and ccc > count_num:
                        flag = 1
                        break
                    base_lst = [url, title, date, headlines, copyright_stat]
                    insert_sqlite(conn, *base_lst)
                    time.sleep(random.randint(1, 3))
                except Exception as e:
                    print(e)
                    print(traceback.format_exc())
                    flag = 1
                    break
                finally:
                    conn.commit()
            if flag == 1:
                break

        conn.commit()
        conn.close()
